#include "mainwindow.h"
#include "ui_mainwindow.h"

using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    widget=new checkOutWidget(nullptr);
    this->n=new netWorkThread(this);
    this->n->start();
    ui->setupUi(this);
    connect(this->ui->pushButton,&QPushButton::clicked,this,&MainWindow::refreshWidget);
    connect(this->ui->pushButton_3,&QPushButton::clicked,this,&MainWindow::setInUsed);
    connect(this->ui->pushButton_2,&QPushButton::clicked,this,&MainWindow::checkOut);
    connect(this->ui->pushButton_4,&QPushButton::clicked,this,&MainWindow::report);
}

MainWindow::~MainWindow()
{
    delete n;
    delete ui;
}

void MainWindow::refreshWidget()
{
    this->ui->treeWidget->clear();
    QTreeWidgetItem *parent = ui->treeWidget->invisibleRootItem();
    for(auto &i:room.room){
        QTreeWidgetItem *item = new QTreeWidgetItem(parent);
        item->setText(0,QString::fromStdString(i.second.roomNo));
        item->setText(2,i.second.isOpen?QString("Yes"):QString("No"));
        item->setText(3,i.second.isServed?QString("Yes"):QString("No"));
        item->setText(1,i.second.sessionId!=0?QString("Yes"):QString("No"));
        item->setText(4,QString().setNum(i.second.wind));
        item->setText(5,QString().setNum(i.second.temperature));
        item->setText(6,QString().setNum(i.second.currentTemperature));
        item->setText(7,QString().setNum(i.second.cost));
        item->setText(8,QString().setNum(i.second.timeLeft));
    }
}

void MainWindow::setInUsed()
{
    QTreeWidgetItem* ret=this->ui->treeWidget->currentItem();
    QString roomNo=ret->text(0);
    room.setSessionId(roomNo.toStdString());
}

void MainWindow::checkOut()
{
    QTreeWidgetItem* ret=this->ui->treeWidget->currentItem();
    QString roomNo=ret->text(0);
    time_t sessionId=room.getSessionId(roomNo.toStdString());
    double cost=room.getCost(roomNo.toStdString());
    widget->fillFrame(roomNo,sessionId,cost);
    room.clearState(roomNo.toStdString());
    widget->show();
}

void MainWindow::report()
{
    QString fileName;
    time_t i=time(nullptr);
    fileName.append("report").append(QString().setNum(i)).append(".txt");
    QString path;
    path.append("/tmp/report/").append(fileName);
    std::ofstream fout(path.toStdString());
    fout<<"Reports:"<<std::endl;
    fout<<"date:"<<QString::fromLatin1(ctime(&i)).toStdString()<<std::endl;
    fout<<"----------------------------------------------------------------"<<std::endl;
    for(auto &i : room.room){
        fout<<"房间号:"<<i.first<<std::endl;
        fout<<"使用次数:"<<(i.second.switchTime/2)<<std::endl;
        auto m_temp=max_element(i.second.tempSta.begin(),i.second.tempSta.end(),
                               [](std::pair<int,int>& l,std::pair<int,int>& r)->bool{return l.second<r.second;});
        fout<<"最常用目标温度:"<<m_temp->first<<std::endl;
        auto m_wind=max_element(i.second.windSta.begin(),i.second.windSta.end(),
                                [](std::pair<int,int>& l,std::pair<int,int>& r)->bool{return l.second<r.second;});


        fout<<"最常用风速:"<<m_wind->first<<std::endl;
        fout<<"达到目标温度次数:"<<i.second.achieveTempTime<<std::endl;
        fout<<"调度次数:"<<i.second.scheduleTime<<std::endl;
        auto instance=statistics::getInstance();
        QSqlQuery query(database::getInstance()->getDB());
        auto ret=query.exec(QString("select count(*) from records where sessionId=%1;")
                   .arg(QString().setNum(i.second.sessionId)));
        assert(ret);
        query.next();
        int detailNum=query.value(0).toInt();
        fout<<"详单数:"<<detailNum<<std::endl;
        fout<<"总费用:"<<i.second.cost<<std::endl;
        fout<<std::endl;
    }
    fout.close();


}
